<?php
/**
 * Created by Yury Patrin.
 * Time: 26.11.13 20:58
 */

class Ajax extends CI_Controller
{
    protected $_users;

    public function __construct()
    {
        parent::__construct();
        $this->load->library('users');
    }

    public function createUser()
    {
        $email = trim($this->input->post('email'));
        $passwd = trim($this->input->post('passwd'));

        if ($passwd == '') {
            $result = ['status' => 'error', 'code' => Users::CODE_USER_PASSWD_EMPTY_ERROR];
        } elseif (filter_var($email, FILTER_VALIDATE_EMAIL)) {
            $result = $this->users->createUser($email, $passwd);
        } else {
            $result = ['status' => 'error', 'code' => Users::CODE_USER_EMAIL_ERROR];
        }

        print json_encode($result);
    }

    public function loginUser()
    {
        $email = $this->input->post('email');
        $passwd = $this->input->post('passwd');

        $result = $this->users->loginUser($email, $passwd);

        print json_encode($result);
    }

    public function logoutUser()
    {
        $this->users->logoutUser();
    }

    public function forgotPwd()
    {
        $email = $this->input->post('email');
        $result = $this->users->sendForgotMail($email);

        print json_encode($result);
    }

    public function flushUserPwd()
    {
        $code   = $this->input->post('flush_code');
        $passwd = $this->input->post('password');

        $result = $this->users->flushPassword($code, $passwd);

        print json_encode($result);
    }
}